<?php

    session_start();

    if (!file_exists("../config.php")) {
        @header("Location: ../install.php");
        die;
    }

    include("../global_conf.php");

    if (isset($_SESSION['logged'])) {
        if (isset($_GET['logout'])) {
            session_destroy();
            @header("Location: login.php");
        }
        @header("Location: index.php");
        die;
    }

    require_once($site->classdir."/DBmanager.php");

    if (isset($_POST['login'])) {
        $res = DBmanager::getInstance()->check_login($_POST['admin'], $_POST['passwd']);
        if ($res) {
            $_SESSION['logged']=$res->id;
            $_SESSION['role']=$res->role;
            $_SESSION['lang']=$res->preferred_lang;
            @header("Location: index.php");
            die;
        }
        else
            $msg="Login failed";
    }

    include("./include/top.inc.php");
?>
<div id="wrapper">
    <div id="header">
        <div id="logo">
            <h1><a href="index.php">Qms</a></h1>
            <p>qr-code management system by <a href="http://www.facebook.com/goatsevethequeen">Lorenzo D'Agostino</a></p>
        </div>
    </div>
    <div id="page">
    <div id="page-bgtop">
    <div id="page-bgbtm">
        <div id="content">
            <?php if (isset($msg)) echo "<div class=\"error\">".$msg."</div>"; ?>
            <div class="post">
            <h2 class="title">Perform login</h2>
            <fieldset>
                <legend>Login</legend>
                <form action="?" method="post" id="loginform">
                    <label>Username</label>
                    <input type="text" id="admintext" name="admin" />
                    <label>Password</label>
                    <input type="password" id="adminpasswd" name="passwd" />
                    <input type="submit" name="login" value="Login" />
                </form>
            </fieldset>
            </div>
        </div>
        <script type="text/javascript" language="javascript">
            document.getElementById("admintext").focus();
        </script>
        <div style="clear: both">&nbsp;</div>
    </div>
    </div>
    </div>
</div>
<?php
    include("./include/foot.inc.php");
?>